local AddPrefabPostInit = AddPrefabPostInit
GLOBAL.setfenv(1, GLOBAL)

local DISTANCE = 20
local MAST_TAGS = { "witch_gisela_mini" }
local function Find_Witch_Gisela_Mini(inst)
    local x, y, z = inst.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x, y, z, DISTANCE, MAST_TAGS)
    if #ents > 0 then
        inst.AnimState:PlayAnimation("active_loop", true)
		inst.AnimState:SetFrame(math.random(inst.AnimState:GetCurrentAnimationNumFrames()) - 1)
    elseif not inst.components.teleporter:IsActive() then
        inst.AnimState:PlayAnimation("inactive")
    end

    for _, ent in ipairs(ents) do
        if ent:IsValid() and ent.components.health and not ent.components.health:IsDead() and inst:IsNear(ent, 2) then
            ent.components.health:DoDelta(-ent.components.health.maxhealth)
        end
    end
end

AddPrefabPostInit("townportaltalisman", function(inst)
    if not TheWorld.ismastersim then
        return
    end

    inst:DoPeriodicTask(1, Find_Witch_Gisela_Mini)
end)
